package com.swf.seed.os.command.impl;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;

import com.swf.seed.base.SdLetter;
import com.swf.seed.base.SystemContants;
import com.swf.seed.os.annotation.Command;
import com.swf.seed.util.SdPatternUtils;

@Command("match")
public class MatchCommand extends CommandAdaptor {

	@Override
	public void option() {
		options.addOption(SdLetter.h, "help", false, "show match usage");
		options.addOption(Option.builder(SdLetter.s).longOpt("string").hasArg().required().argName("string").desc("the string").build());
		options.addOption(Option.builder(SdLetter.p).longOpt("pattern").hasArg().required().argName("pattern").desc("the regex pattern").build());
		String flagDesc = " Match flags, a bit mask that may include "+SystemContants.LINE_SEPARATOR+
				" {@link #CASE_INSENSITIVE 02}, {@link #MULTILINE 08}, {@link #DOTALL 20},"+SystemContants.LINE_SEPARATOR+
     "{@link #UNICODE_CASE 40}, {@link #CANON_EQ 80}, {@link #UNIX_LINES 01},"+SystemContants.LINE_SEPARATOR+
     "{@link #LITERAL 10} and {@link #COMMENTS 04}";
		options.addOption(Option.builder(SdLetter.f).longOpt("flag").hasArg().valueSeparator().argName("flag").desc(flagDesc).build());

	}

	@Override
	void customizedExecute(CommandLine commandLine) throws ParseException {
		String str = commandLine.getOptionValue(SdLetter.s);
		String pattern = commandLine.getOptionValue(SdLetter.p);
		String flag = commandLine.getOptionValue(SdLetter.f);
		boolean matches = SdPatternUtils.matches(str, pattern, flag==null?0:Integer.parseInt(flag));
		System.out.println("match:"+matches);
	}

}
